package edu.ccut.saturn.component.xml;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class SaturnElement implements ISaturnElement {

	String elementName;

	Map<String, String> attributes = new HashMap<String, String>();

	List<ISaturnElement> subElement = new ArrayList<ISaturnElement>();

	public SaturnElement(String elementName) {
		this.elementName = elementName;
	}

	public SaturnElement(String elementName, String text) {
		this.elementName = elementName;
		SaturnTextElement saturnTextElement = new SaturnTextElement(text);
		this.subElement.add(saturnTextElement);
	}

	public String getElementName() {
		return elementName;
	}

	public void setElementName(String elementName) {
		this.elementName = elementName;
	}

	public Map<String, String> getAttributes() {
		return attributes;
	}

	public void setAttributes(Map<String, String> attributes) {
		this.attributes = attributes;
	}

	public List<ISaturnElement> getSubElement() {
		return subElement;
	}

	public void setSubElement(List<ISaturnElement> subElement) {
		this.subElement = subElement;
	}

	public void addAttribute(String key, String value) {
		this.attributes.put(key, value);
	}

	public void removeAttribute(String key) {

		if (this.attributes.containsKey(key)) {
			this.attributes.remove(key);
		}
	}

	public void addSubElement(ISaturnElement saturnElement) {
		this.subElement.add(saturnElement);
	}

	public String getXMLStr() {
		StringBuffer buffer = new StringBuffer();
		buffer.append(XMLHelper.getElementStartTagWithAttributes(elementName,
				attributes));

		for (ISaturnElement element : this.subElement) {
			buffer.append(element.getXMLStr());
		}
		buffer.append(XMLHelper.getElementTailTag(elementName));
		return buffer.toString();
	}

}
